using System;
using System.Windows.Forms;
using Curriculum.DataSet;
using DevExpress.XtraEditors;
using E4RPTools;

namespace Curriculum.Controls
{
    public partial class PositionXtraForm : XtraForm
    {
        private long _documentId;
        private BackOfficeController _controller;
        private PositionDataSet _dataSet;

        #region Properties

        public long DocumentId
        {
            set { _documentId = value; }
        }

        public BackOfficeController Controller
        {
            set { _controller = value; }
        }

        #endregion

        public PositionXtraForm()
        {
            InitializeComponent();
            Load += PositionXtraFormLoad;
            oKSimpleButton.Click += OKSimpleButtonClick;
        }

        #region Private Methods

        private void MergeData()
        {
            _dataSet = new PositionDataSet();
            if (_documentId <= 0)
            {
                var position = _dataSet.Position.NewPositionRow();
                position.Id = _documentId;
                _dataSet.Position.AddPositionRow(position);
            }
            else
                _dataSet.Merge(_controller.GetPosition(_documentId));
        }

        private void SetDataToControls()
        {
            areaTextEdit.Text = _dataSet.Position[0].IsAreaNull() ? "" : _dataSet.Position[0].Area;
            subAreaTextEdit.Text = _dataSet.Position[0].IsSubAreaNull() ? "" : _dataSet.Position[0].SubArea;
            nameTextEdit.Text = _dataSet.Position[0].IsNameNull() ? "" : _dataSet.Position[0].Name;
            
        }

        private void GetDataFromControls()
        {
            _dataSet.Position[0].Area = areaTextEdit.Text;
            _dataSet.Position[0].SubArea = subAreaTextEdit.Text;
            _dataSet.Position[0].Name = nameTextEdit.Text;
        }

        private void ClearControlErrors()
        {
            dxErrorProvider.SetError(areaTextEdit, "");
            dxErrorProvider.SetError(subAreaTextEdit, "");
            dxErrorProvider.SetError(nameTextEdit, "");
            
        }

        private Control GetControl(string reference)
        {
            switch (reference)
            {
                case "Area":
                    return areaTextEdit;
                case "SubArea":
                    return subAreaTextEdit;
                case "Name":
                    return nameTextEdit;                
                default:
                    return null;
            }
        }

        private void SetControlErrors(Result result)
        {
            foreach (var error in result.Errors)
                dxErrorProvider.SetError(GetControl(error.Reference), error.Message);
        }

        #endregion

        void PositionXtraFormLoad(object sender, EventArgs e)
        {
            MergeData();
            SetDataToControls();
        }

        void OKSimpleButtonClick(object sender, EventArgs e)
        {
            ClearControlErrors();
            GetDataFromControls();
            var result = _controller.IsPositionValid(_dataSet);
            if (result.Failed)
            {
                DialogResult = DialogResult.None;
                SetControlErrors(result);
            }
            else
                _controller.SavePosition(_dataSet);
        }
    }
}